<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<div class="easyui-layout" style="width: 100%; height: 100%;" fit="true">
	
	<div id="toolbar" style="display: none;">
		<table>
			<tr>
				<td><a href="javascript:void(0);" class="easyui-linkbutton"
					data-options="iconCls:'ext-icon-note_add',plain:true"
					onclick="openAddWindow();">添加</a></td>
				<td><a onclick="redoFun();" href="javascript:void(0);"
					class="easyui-linkbutton"
					data-options="plain:true,iconCls:'ext-icon-resultset_next'">展开</a><a
					onclick="undoFun();" href="javascript:void(0);"
					class="easyui-linkbutton"
					data-options="plain:true,iconCls:'ext-icon-resultset_previous'">折叠</a></td>
				<td><a onclick="tbl.treegrid('reload');"
					href="javascript:void(0);" class="easyui-linkbutton"
					data-options="plain:true,iconCls:'ext-icon-arrow_refresh'">刷新</a>
			</tr>
		</table>
		<table>
			<tr>
				<td><div id="sysType"></div></td>
			</tr>
		</table>
		<input type="hidden" id="typeId" name="typeId"/>
		<input type="hidden" id="typeName" name="typeName"/>
	</div>

	<div data-options="region:'center',split:true"
		style="width: 100%; height: 100%">
		<table id="tableGrid"></table>
	</div>
</div>
<script>
	var redoFun = function() {
		var node = tbl.treegrid('getSelected');
		if (node) {
			tbl.treegrid('expandAll', node.id);
		} else {
			tbl.treegrid('expandAll');
		}
	};
	var undoFun = function() {
		var node = tbl.treegrid('getSelected');
		if (node) {
			tbl.treegrid('collapseAll', node.id);
		} else {
			tbl.treegrid('collapseAll');
		}
	};

	function openAddWindow() {
		var params = {
			"typeId" : $('#typeId').val(),
			"typeName" : $('#typeName').val()
		}
		$.pmsDialog({
			id : "w_form",
			title : '新增',
			method : 'post',
			params : params,
			href : '<c:url value="/views/jsp/sys/form.jsp"/>',
			width : 500,
			height : 500
		});
	}

	function openUpdateWindow(newsTypeId, parentid) {
		var params = {
			"newsTypeId" : newsTypeId,
			"parentid" : parentid,
			"typeId" : $('#typeId').val(),
			"typeName" : $('#typeName').val()
		}
		
		$.pmsDialog({
			id : "w_form",
			title : '修改',
			method : 'post',
			params : params,
			href : '<c:url value="/views/jsp/sys/form.jsp"/>',
			width : 500,
			height : 500
		});
	}

	function deleteRecord(newsTypeId, i) {
		if (confirm("确定删除记录?")) {
			
			var param = {
				"newsTypeId" : newsTypeId
			};
			
			
			$.pmsAjax({
				url : '<c:url value="/newstype/deleteEmBNewsType.do"/>',
				data : param,
				success : function(data) {
					if (data.code == 200) {
						alertShow("删除记录成功");
						/*
						疑问：tbl.removeRow(i)；没有调用成功，目前只能整个页面刷新
						 */
						//tbl.removeRow(i);
						initList();

					} else {
						alertError(data.resultMessage);
					}
				}
			});
		}
	}
	
	function changeType(typeId,typeName){
		$('#typeId').val(typeId);
		$('#typeName').val(typeName);
		initList(typeId);
	}

	var tbl;
	
	$(function() {
		$.post('<c:url value="/type/searchAllEmBType.do"/>',
        function(data) {
			var str = '';
			for(i=0;i<data.length;i++){
				if(i==0){
					str +='<a href="javascript:changeType('+data[i].typeId+',\''+data[i].typeName+'\')" class="easyui-linkbutton systype" data-options="selected:true,plain:true,toggle:true,group:\'g1\'">'+data[i].typeName+'</a>'
				
					changeType(data[i].typeId,data[i].typeName);
				}else{
					str +='<a href="javascript:changeType('+data[i].typeId+',\''+data[i].typeName+'\')" class="easyui-linkbutton systype" data-options="plain:true,toggle:true,group:\'g1\'">'+data[i].typeName+'</a>'
				}
			}
			$("#sysType").html(str);
			$.parser.parse($('#sysType'));
			
			
			
        },
        'json');
		
	});

	function initList(typeId) {
		
		params = {};
		params.typeId = typeId;
		tbl = $('#tableGrid')
				.treegrid(
						{
							toolbar : '#toolbar',
							columns : [ [
									{
										field : 'name',
										title : '分类名称',
										width : 400
									},
									{
										field : 'isIndex',
										title : '首页',
										width : 50,
										align:'center',
										formatter : function(value, row) {
											str = '';
											if(value){
												str = "是";
											}else{
												str = "否";
											}
											return str;
										}
									},
									{
										field : 'isShow',
										title : '显示',
										width : 50,
										align:'center',
										formatter : function(value, row) {
											str = '';
											if(value){
												str = "是";
											}else{
												str = "否";
											}
											return str;
										}
									},
									{
										title : '操作',
										field : 'action',
										width : '80',
										align : 'center',
										formatter : function(value, row) {
											var str = '';
											str += ' <img class="iconImg ext-icon-note_edit" title="编辑" onclick="openUpdateWindow('
													+ row.newsTypeId
													+ ','
													+ row.parentid + ');"/>';
											str += ' <img class="iconImg ext-icon-note_delete" title="删除" onclick="deleteRecord('
													+ row.newsTypeId + ');"/>';
											return str;
										}
									} ] ],
							url : '<c:url value="/newstype/searchEmBNewsTypeByParentId.do"/>',
							queryParams:params,
							fitColumns : false,
							singleSelect : true,
							idField : 'id',
							treeField : 'name',
							collapsible : true,
							completeCallback : function(data) {
								if (data.resultCode == "100") {
									alertError(data.errorMsg);
								}
							},
							onDblClickRow : function(row) {
								openUpdateWindow(row.newsTypeId, row.parentid);
							}
						});
	}
</script>
